<template>
  <el-dialog :title="formData.id?'编辑':'添加'" :visible.sync="modelShow">
    <el-form ref="couponForm" :model="formData" label-width="120px" :rules="formRules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username"/>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="formData.phone"/>
      </el-form-item>
      <el-form-item>
        <el-button @click="modelShow = false">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { saveOrUpdateOrApi } from '@/api/user'
export default {
  name: 'SysUserForm',
  data(){
    return{
      uploadBase:'colaBoot',
      modelShow:false,
      couponLogo:'',
      formData:{},
      formRules:{
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 90, message: '长度在 3 到 90 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    opened(isEdit,row){
      this.formData = isEdit ? row:{};
      this.modelShow = true;
    },
    submitForm(){
      this.$refs['couponForm'].validate((valid) => {
        if (valid) {
          saveOrUpdateOrApi(this.formData).then(res => {
            this.modelShow = false
            this.$emit('refreshTable');
          })
        }
      })
    },
    handleBgImgSuccess(res, file){
      this.couponLogo = res.data;
      this.formData.logo = res.data;
    },
    beforeBgImgUpload(file){
      const isIMG = file.type.substr(0,5) === 'image';
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isIMG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt5M) {
        this.$message.error('上传头像图片大小不能超过 5MB!');
      }
      return isIMG && isLt5M;
    },
  }
}
</script>

<style scoped>

</style>
